//
//  ViewController.m
//  等间隔排列-Masonry
//
//  Created by chenleping on 2018/6/6.
//  Copyright © 2018年 IAPTest. All rights reserved.
//

#import "ViewController.h"
#import <Masonry.h>
@interface ViewController ()

@property (nonatomic, strong) NSMutableArray * masonryViewArray;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor greenColor];
}

// 1、水平方向排列、固定控件间隔、控件长度不定
- (void)test_masonry_horizontal_fixSpace {
    // 实现masonry水平固定间隔方法
    /*
     fixedSpacing:视图间的间距
     leadSpacing:最左侧视图距离父视图左边距
     tailSpacing:最右侧视图距离父视图右边距
     */
    [self.masonryViewArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:30 leadSpacing:5 tailSpacing:5];
    // 设置array的垂直方向的约束
    [self.masonryViewArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(200);  // 距离父视图上方距离
        make.height.mas_equalTo(50);    // 高度
    }];
}
// 2、水平方向排列、固定控件长度、控件间隔不定
- (void)test_masonry_horizontal_fixItemWidth {
    
    // 实现masonry水平固定控件宽度方法
    [self.masonryViewArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:50 leadSpacing:10 tailSpacing:10];
    // 设置array的垂直方向的约束
    [self.masonryViewArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(150);
        make.height.mas_equalTo(80);
    }];
}
// 3、垂直方向排列、固定控件间隔、控件高度不定
- (void)test_masonry_vertical_fixSpace {
    // 实现masonry垂直固定控件高度方法
    [self.masonryViewArray mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:30 leadSpacing:100 tailSpacing:10];
    // 设置array的水平方向的约束
    [self.masonryViewArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(150);
        make.width.mas_equalTo(80);
    }];
}
- (IBAction)action1:(id)sender {
    _masonryViewArray = nil;
    for (UIView * subviews in self.view.subviews) {
        if (![subviews isKindOfClass:[UIButton class]]) {
            [subviews removeFromSuperview];
        }
    }
    [self test_masonry_horizontal_fixSpace];
}
- (IBAction)action2:(id)sender {
    _masonryViewArray = nil;
    for (UIView * subviews in self.view.subviews) {
        if (![subviews isKindOfClass:[UIButton class]]) {
            [subviews removeFromSuperview];
        }
    }
    [self test_masonry_horizontal_fixItemWidth];
}
- (IBAction)action3:(id)sender {
    _masonryViewArray = nil;
    for (UIView * subviews in self.view.subviews) {
        if (![subviews isKindOfClass:[UIButton class]]) {
            [subviews removeFromSuperview];
        }
    }
    [self test_masonry_vertical_fixSpace];
}

- (NSMutableArray *)masonryViewArray {
    
    if (!_masonryViewArray) {
        _masonryViewArray = [NSMutableArray array];
        for (int i = 0; i < 5; i ++) {
            UIView *view = [[UIView alloc] init];
            view.backgroundColor = [UIColor redColor];
            [self.view addSubview:view];
            [_masonryViewArray addObject:view];
        }
    }
    return _masonryViewArray;
}


@end
